<template>
  <div class="xtx-pay-page">
    <div class="container">
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem>支付订单</XtxBreadItem>
      </XtxBread>
      <!-- 付款信息 -->
      <div class="pay-info" v-if="order">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p v-if="order.countdown > 0">
            支付还剩<span>{{ branch }}分:{{ second }}秒</span> , 超时后将取消订单
          </p>
          <p v-else>支付超时！</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ order.payMoney }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <!-- target="_blank":新开一个页面，不关闭跳转前的页面 -->
          <a class="btn alipay" @click="visibleDialog = true" :href="payUrl" target="_blank"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
    <XtxDialog title="正在支付..." v-model:visible="visibleDialog">
      <div class="pay-wait">
        <img src="@/assets/images/images/load.gif" alt="" />
        <div v-if="order">
          <p>如果支付成功：</p>
          <a href="javascript:;"> 查看订单详情> </a>
          <p>如果支付失败：</p>
          <a href="javascript:;">查看相关疑问></a>
        </div>
      </div>
    </XtxDialog>
  </div>
</template>
<script>
import { onMounted, ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { getOrderInfo } from '@/api/order'
import { baseURL } from '@/utils/request.js'
export default {
  name: 'XtxPayPage',
  setup() {
    const store = useStore()
    const route = useRoute()
    const order = ref({})
    onMounted(async () => {
      try {
        const { result } = await getOrderInfo(route.query.id)
        order.value = result
        order.value.payMoney = result.payMoney.toFixed(2)
        // 获取分秒数据，供后续定时器使用
        second.value = order.value.countdown % 60
        if (order.value.countdown % 60) {
          branch.value = (order.value.countdown - (order.value.countdown % 60)) / 60
        } else {
          branch.value = order.value.countdown / 60
        }
        // 及时更新头部购物车的徽标数据
        await store.dispatch('cart/getCartList')
      } catch {}
    })
    // 支付倒计时
    const second = ref(null) // 秒
    const branch = ref(null) // 分
    const num = setInterval(async () => {
      if (branch.value === 0 && second.value === 0) {
        clearInterval(num)
        const { result } = await getOrderInfo(route.query.id)
        order.value = result
      }
      if (second.value === 0) {
        if (branch.value - 1 !== -1) {
          branch.value--
          second.value = 60
        }
      }
      second.value--
    }, 1000)
    // 支付流程
    // 1.点击支付方式后，a标签设置新开页，根据后台地址携带订单id和回跳地址
    // 2.后台服务器跳转到支付方式页面比如支付宝，支付成功后回跳后台，后台跳到你传递的回跳地址
    // 3.结果页（也就是传递的回跳地址）根据回跳地址上的订单id，查询支付状态，然后展示支付状态

    // 跳转支付
    // 1.使用request.js的后台地址
    // 2.拼接支付地址（订单id和回跳地址）给a标签使用，打开新标签
    // 3.去支付的同时当前订单页需弹窗提示正在支付
    const payUrl = computed(() => {
      if (order.value.id) {
        // 前端服务器，支付结果地址
        // encodeURIComponent：把地址转成url编码格式，处理特殊字符
        const redirectUrl = encodeURIComponent('http://localhost:8080/#/user/pay/result')
        return `${baseURL}pay/aliPay?orderId=${order.value.id}&redirect=${redirectUrl}`
      }
      return 'javascript;;'
    })
    const visibleDialog = ref(false)
    return { order, second, branch, payUrl, visibleDialog }
  }
}
</script>
<style scoped lang="less">
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  .icon {
    font-size: 80px;
    color: #1dc779;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }
      &:last-child {
        color: var(--price-color);
        font-size: 20px;
      }
    }
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;
  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;
    &.active,
    &:hover {
      border-color: var(--xtx-color);
    }
    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat
        center / contain;
    }
    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat
        center / contain;
    }
  }
}
.pay-wait {
  display: flex;
  justify-content: space-around;
  p {
    margin-top: 30px;
    font-size: 14px;
  }
  a {
    color: var(--xtx-color);
  }
}
</style>
